
local meta = FindMetaTable( "Player" );

function meta:TransferToNewCharacter( name, classid, age, desc, model )

	self:SetPlayerDidInitialCC( true );

	self:ApplyMovementSpeeds();
	self:MakeInvisible( false );
	
	self:SetModel( model );

	self:CharacterInitialize();

	self:SetNWString( "RPName", name );
	self:SetPlayerClass( classid );
	self:SetPlayerAge( age );
	self:SetPlayerPhysDesc( desc );
	self:SetPlayerOriginalModel( model );

	-- INITIAL CHARACTER SAVING
	self:sqlCreateCharacterSave();
	
	self:Freeze( false );
	
	self:KillSilent();
	self:Spawn();

	umsg.Start( "PCT" );
		umsg.Short( self:EntIndex() );
	umsg.End();

end

function meta:GetRPName()

	return self:GetNWString( "RPName" );

end

function meta:RPNick()

	return self:GetNWString( "RPName" );

end

function meta:GiveHealth( amt )

	self:SetHealth( math.Clamp( self:Health() + amt, 0, self:GetTable().MaxHealth ) );

end

function meta:TakeHealth( amt )

	self:SetHealth( math.Clamp( self:Health() - amt, 0, self:GetTable().MaxHealth ) );

	if( self:Health() <= 0 ) then
	
		self:Kill();
	
	end	

end

function meta:UpdateAdminFlags()

	self:GetTable().AdminFlags = GAMEMODE.ServerAdmins[self:SteamID()] or "";

	if( self:IsSuperAdmin() ) then
	
		self:GetTable().AdminFlags = "+";
	
	end

end

function meta:HolsterWeaponIfPossible()

	local weap = self:GetActiveWeapon();
	
	if( weap and weap:IsValid() ) then

		if( weap:GetTable().HolsteredAtStart ) then

			self:SetPlayerHolstered( true );
		
		else
		
			self:SetPlayerHolstered( false );
		
		end
	
	end

end

function meta:TakeHealth( amt )

	if( amt >= self:Health() ) then
		
		self:Kill();
		return;
	
	end

	self:SetHealth( self:Health() - amt );

end


--[[
--======================================--
-- HANDLE JUMP FALL                     --
--======================================--

This function is called everytime the player falls from a distance.

190 is the minimum distance for the player's movement to be stopped momentarily after a fall.

]]--
function meta:HandleJumpFall( dist )

	if( dist >= 190 ) then
	
		--Imitate the effect of falling and stopping.
	
		self:ConCommand( "+duck" );
		self:StopMovement();	
		
		self:ViewPunch( Angle( 1, 1, 0 ) * 15 );
		
		timer.Simple( .4, function()
		
			self:ConCommand( "-duck" );
			self:ApplyMovementSpeeds();
		
		end );
	
		if( dist > 280 ) then

			local dmg = ( dist - 280 ) * 1.3;
		
			local rmul = math.Rand( .5, 1.3 );
			local lmul = math.Rand( .5, 1.3 );
			
			self:SetPlayerLLegHP( math.Clamp( self:GetPlayerLLegHP() - dmg * lmul, 0, 100 ) );
			self:SetPlayerRLegHP( math.Clamp( self:GetPlayerRLegHP() - dmg * rmul, 0, 100 ) );
			
			self:TakeHealth(( dist - 280 ) * .5 );
				
		end
	
	else
	
		self:ViewPunch( Angle( 1, 0, 0 ) * 4 );
	
	end

end



